<?php

namespace app\admin\model\trades\order;

use think\Db;
use think\Model;


class Gain extends Model
{

    

    

    // 表名
    protected $table = 'activity_gain';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'ctime_text'
    ];
    

    



    public function getCtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['ctime']) ? $data['ctime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /*
     *查找收益表信息
     */
    public function  getGainInfo($account_id){
        $where['account_id'] = $account_id;
        $r = $this->getModel()->where($where)->field('*')->find();
        $ar = [];
        if($r){
            $ar['total'] = $r->total;
            $ar['balance'] = $r->balance;
            $ar['temp'] = $r->temp;
            unset($r);
            return $ar;
        }else
            return '';

    }

    /*
     * 收益总表
     */
    public static function update_gain($admin_id,$amount){
            Db::name('activity_gain')
            ->where('account_id',$admin_id)
            //->data($up)
            ->inc('temp',$amount)
            ->inc('total',$amount)
            ->update();
            return true;
    }

}
